package cn.chinaflame.pagodas.service;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.dto.PaMemberDtoNew;
import cn.chinaflame.pagodas.dto.PaMemberOldDto;
import cn.chinaflame.pagodas.repository.PaMemberRepository;
import cn.chinaflame.pagodas.req.BaseReq;
import cn.chinaflame.pagodas.req.PaMemOldReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author created by lbq
 * @date 2018年5月3日 下午5:14:38
 **/
@Service
public class PaMemberServiceNew {
    private final static Logger log = LoggerFactory.getLogger(PaMemberService.class);
    @Autowired
    private PaMemberRepository paMemberRepository;
    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private PaAgentService paAgentService;

    public Result<List<PaMemberDtoNew>> queryByPage(BaseReq req) {
        log.info("会员按代理分页查询【service】req:{}", req);
        String sql = "SELECT a.nick_name as nickName ,a.mobile,a.balance,a.last_consume_time as lastConsumeTime from pa_member a ORDER BY a.balance desc";

        JdbcPage<PaMemberDtoNew> page = new JdbcPage<>(sql, req.getPageSize(), req.getPageIndex(), jdbcTemplate, PaMemberDtoNew.class);
        return Result.createPage(page.getPage(), req.getPageSize(), req.getPageIndex(), page.getCount());

    }

    public Result<List<PaMemberOldDto>> queryOrderOld(PaMemOldReq req) {
        req.check();
        String sql = "SELECT a.nick_name as nickName, a.mobile,b.id,d.`name` as productName,"
                + "c.start_time as startTime,c.end_time as endTime,b.money,b.pay_type as payType, b.pay_status as payStatus "
                + "from pa_member a INNER JOIN pa_order b on (a.mobile=b.mobile) "
                + "INNER JOIN pa_orderitem c ON (c.order_id=b.id) "
                + "INNER JOIN pa_product d ON(d.id=c.product_id) "
                + " where a.mobile = '" + req.getMobile() + "'";
        JdbcPage<PaMemberOldDto> page = new JdbcPage<>(sql, req.getPageSize(), req.getPageIndex(), jdbcTemplate, PaMemberOldDto.class);
        return Result.createPage(page.getPage(), req.getPageSize(), req.getPageIndex(), page.getCount());
    }
}
